package com.sms.biz.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.sms.biz.IFilterStrBiz;
import com.sms.dao.IFilterStrDAO;
import com.sms.po.FilterStr;

/**
 * @author 20111012 by cxh
 */
public class FilterStrBizImpl implements IFilterStrBiz {
	private IFilterStrDAO ibld = null;
	public void setIbld(IFilterStrDAO ibld){ this.ibld = ibld; }
	@Override
	public List<FilterStr> showAllFilterStr() {
		return ibld.showAllFilterStr();
	}

	@Override
	public List<FilterStr> showAllFilterStrByPage(String content, int start, int limit) {
		// 离线查询
		DetachedCriteria criteria = DetachedCriteria
				.forClass(FilterStr.class);
		// 如果条件标题不为空,那么将条件标题附加上
		if (!"".equals(content) && null != content) {
			criteria.add(Restrictions.like("content", content,
					MatchMode.ANYWHERE));
		}
		if (start <= 0) {start = 0;		}
		if (limit <= 0) {limit = 10;	}
		return ibld.showAllFilterStrByPage(criteria, start, limit);
	}
	@Override
	public int getAllFilterStrCount(String content) {
		// 离线查询
		DetachedCriteria criteria = DetachedCriteria
				.forClass(FilterStr.class);
		// 如果条件标题不为空,那么将条件标题附加上
		if (!"".equals(content) && null != content) {
			criteria.add(Restrictions.like("content", content,
					MatchMode.ANYWHERE));
		}
		return ibld.getAllFilterStrCount(criteria);
	}
	@Override
	public boolean addFilterStr(FilterStr fs) {
		try {
			ibld.addFilterStr(fs);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	@Override
	public boolean removeFilterStr(FilterStr fs) {
		return ibld.removeFilterStr(fs);
//		try {
//			ibld.removeFilterStr(fs);
//			return true;
//		} catch (Exception e) {
//			return false;
//		}
	}
	@Override
	public boolean updateFilterStr(FilterStr fs) {
//		return ibld.updateFilterStr(bl);
		try {
			ibld.updateFilterStr(fs);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public String filterContent(String content){
		List<FilterStr> filterStrList = ibld.showAllFilterStr();
		
		for(int i=0;i<filterStrList.size();i++)
		{
			String keyword = filterStrList.get(i).getContent().trim();			
			if(content.indexOf(keyword)>=0)return keyword;
		}
		return "";
	}

}
